//
//  ChangeTelViewController.m
//  fxxiaoke
//
//  Created by 鞠超 on 16/9/10.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "ChangeTelViewController.h"

#import "CaptchaViewController.h"

@interface ChangeTelViewController () <UIScrollViewDelegate, UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView * scrollView;

@property (nonatomic, strong) UITextField * text_Tel;

@end

@implementation ChangeTelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    
    _lblTitle.text = @"更换手机号";
    
    //左返回
    UIImageView * image_LeftRetrun = [[UIImageView alloc] initWithFrame:CGRectMake(5, 31.5, 25, 25)];
    image_LeftRetrun.userInteractionEnabled = YES;
    image_LeftRetrun.image = [UIImage imageNamed:@"Left-return.png"];
    [_topView addSubview:image_LeftRetrun];
    
    UILabel * lbl_return = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_LeftRetrun.frame) - 6, 35, 90, 15)];
    lbl_return.text = @"绑定手机号";
    lbl_return.textColor = [UIColor whiteColor];
    lbl_return.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:lbl_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(5, 22, 105, 40);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
    
    [self.text_Tel becomeFirstResponder];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    self.scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.scrollView.contentSize = CGSizeMake(0, SCREEN_HEIGHT - 64);
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    
    UIView * view_Tel = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    view_Tel.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:view_Tel];
    
    //下面的线
    UIView * view_Line = [[UIView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, 0.5)];
    view_Line.backgroundColor = [UIColor grayColor];
    [self.scrollView addSubview:view_Line];
    
    UILabel * lbl_86 = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 40, 20)];
    lbl_86.text = @"+ 86";
    lbl_86.textColor = [UIColor blackColor];
    lbl_86.font = [UIFont systemFontOfSize:17.0f];
    lbl_86.textAlignment = NSTextAlignmentRight;
    [view_Tel addSubview:lbl_86];
    
    UIView * view_MiddleLine = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 4, 5, 1, 40)];
    view_MiddleLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [view_Tel addSubview:view_MiddleLine];
    
    //
    self.text_Tel = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(view_MiddleLine.frame) + 10, 12.5, SCREEN_WIDTH - CGRectGetMaxX(view_MiddleLine.frame) - 25, 25)];
    self.text_Tel.placeholder = @"请输入手机号";
    self.text_Tel.font = [UIFont systemFontOfSize:17.0f];
    self.text_Tel.delegate = self;
    self.text_Tel.keyboardType = UIKeyboardTypePhonePad;
    [view_Tel addSubview:self.text_Tel];
    
    //
    UIButton * btn_Next = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_Next.frame = CGRectMake(15, CGRectGetMaxY(view_Tel.frame) + 20, SCREEN_WIDTH - 30, 40);
    btn_Next.layer.cornerRadius = 5;
    [btn_Next setTitle:@"下一步" forState:(UIControlStateNormal)];
    [btn_Next setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    btn_Next.titleLabel.font = [UIFont systemFontOfSize:17.0f];
    btn_Next.backgroundColor = [UIColor colorWithRed:45/255.0 green:150/255.0 blue:248/255.0 alpha:1.0f];
    [self.scrollView addSubview:btn_Next];
    [btn_Next addTarget:self action:@selector(btn_NextAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

#pragma mark - 点击下一步
- (void )btn_NextAction:(UIButton *)sender
{
//    NSLog(@"点击下一步");
    
    if([self.text_Tel.text length] == 0)
    {
        [SVProgressHUD setMinimumDismissTimeInterval:0.9f];
        [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
        [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:20/255.0 green:20/255.0 blue:20/255.0 alpha:0.8f]];
        [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
        [SVProgressHUD showImage:nil status:@"请输入手机号"];
    }
    else
    {
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"确认手机号码" message:[NSString stringWithFormat:@"我们将发送短信验证码至: +86 %@",self.text_Tel.text] preferredStyle:(UIAlertControllerStyleAlert)];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
        UIAlertAction * action_Cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            [self.text_Tel becomeFirstResponder];
        }];
        [alert addAction:action_Cancel];
        
        UIAlertAction * action_Ok = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            if([self.text_Tel.text length] == 11)
            {
                [self.text_Tel resignFirstResponder];
                //模态页面
                CaptchaViewController * captchaViewController = [[CaptchaViewController alloc] init];
                captchaViewController.str_Tel = self.text_Tel.text;
                [self presentViewController:captchaViewController animated:YES completion:^{
                }];
            }
            else
            {
                [SVProgressHUD setMinimumDismissTimeInterval:0.9f];
                [SVProgressHUD setDefaultStyle:SVProgressHUDStyleCustom];
                [SVProgressHUD setBackgroundColor:[UIColor colorWithRed:20/255.0 green:20/255.0 blue:20/255.0 alpha:0.8f]];
                [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
                [SVProgressHUD showImage:nil status:@"手机号格式错误"];
            }
        }];
        [alert addAction:action_Ok];
    }
}




















@end
